C++ String(字符串)和 float/double (浮点数)互转

您所在的位置:网站首页 protobuffer3 string转化 C++ String(字符串)和 float/double (浮点数)互转

C++ String(字符串)和 float/double (浮点数)互转

2024-07-14 10:43| 来源: 网络整理| 查看: 265

C++ Vector(向量)c++ 关键字和标识符 C++ String(字符串)和 float/double (浮点数)互转

在本教程中,我们将借助示例学习如何将字符串转换为浮点数,反之亦然。

C ++字符串与浮点数和双浮点数转换

将字符串转换为浮点数的最简单方法是使用以下C ++ 11函数:

std :: stof() - 将string转换为float

std :: stod() - 将string转换为double

std :: stold() - 将string转换为long double。

这些函数在string头文件中定义。

示例1:C ++字符串转换为浮点和双浮点数#include  #include  int main() {     std::string str = "123.4567";     // 将字符串转换为浮点数     float num_float = std::stof(str);     // 将字符串转换为双浮点数 double     double num_double = std::stod(str);    std:: cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3